
#include "system_para.h"

//*********************************************************************************

#define CONFIG_SAVE_PATH   ("./system_para.tmp")
//*********************************************************************************

static system_para_t  system_para;

//*********************************************************************************




//*********************************************************************************

int system_para_default(void)
{
	memset(&system_para,0,sizeof(system_para_t));

	system_para.audio_para.volume 	   = 100;	
	//******************************************************

	system_para.video_para.is_show     = 0;
	system_para.video_para.path        = 0;
	system_para.video_para.play_mode   = PLAY_MODE_ALL_LOOP;
	system_para.video_para.de_time     = 0;
	system_para.video_para.play_index  = 0;

	sprintf(system_para.video_para.play_path,"%s","/home/video/");

	//******************************************************
	system_para.photo_para.path         = 0;
	system_para.photo_para.play_mode    = PLAY_MODE_ALL_LOOP;
	system_para.photo_para.de_time      = 0;
	system_para.photo_para.play_edp_index   = 0;
	system_para.photo_para.play_hdmi_index  = 0;

	sprintf(system_para.photo_para.play_path,"%s","/home/photo/");

	//******************************************************

	

	return 0;
}

int system_para_config(void)
{
	if(system_para.video_para.path == 0)
	{
		system_para.video_para.play_mode   = PLAY_MODE_ALL_LOOP;
	}

	if(system_para.video_para.path == 1)
	{
		system_para.video_para.play_mode   = PLAY_MODE_ALL_LOOP;
	}
	
	if(system_para.video_para.path == 2)
	{
		system_para.video_para.play_mode   = PLAY_MODE_ALL_LOOP;
	}
	
	if(system_para.video_para.path == 3)
	{
		system_para.video_para.play_mode   = PLAY_MODE_ALL_LOOP;
	}

	if(system_para.video_para.path == 4)
	{
		system_para.video_para.play_mode   = PLAY_MODE_ALL_LOOP;
	}

	return 0;
}

int audio_para_set_volume(char volume)
{
	if(volume < MIN_VOLUME_VAL)
		return -1;
	
	if(volume > MAX_VOLUME_VAL)
		return -1;

	system_para.audio_para.volume = volume;	

	return 0;
}

char audio_para_get_volume(void)
{
	return system_para.audio_para.volume;
}


int video_para_set_path(char* path)
{
	if(!path){
		return -1;
	}

	log_n("path=%s\n",path);

	memset(system_para.video_para.play_path,0,sizeof(system_para.video_para.play_path));

	memcpy(system_para.video_para.play_path,path,strlen(path));

	return 0;
}

char video_para_get_path(char *path)
{
	if(!path){
		return -1;
	}
	
	memcpy(path,system_para.video_para.play_path,strlen(system_para.video_para.play_path));

	return 0;
}

char video_para_get_playmode(void)
{
	return system_para.video_para.play_mode ;
}

int  video_para_set_detime(int de_time)
{
	system_para.video_para.de_time = de_time ;
	
	return 0;
}

int  video_para_get_detime(void)
{	
	return  system_para.video_para.de_time;
}

int  video_para_set_playindex(int index)
{
	system_para.video_para.play_index = index ;
	
	return 0;
}

int  video_para_get_playindex(void)
{	
	return  system_para.video_para.play_index;
}

int  video_para_set_is_show(int val)
{
	system_para.video_para.is_show = val ;
	
	return 0;
}

int  video_para_get_is_show(void)
{	
	return  system_para.video_para.is_show;
}

//******************************************************

int  photo_para_set_playindex(int index)
{
	system_para.photo_para.play_edp_index = index ;
	
	return 0;
}

int  photo_para_get_playindex(void)
{	
	return  system_para.photo_para.play_edp_index;
}


int  photo_para_set_playindex2(int index)
{
	system_para.photo_para.play_hdmi_index = index ;
	
	return 0;
}

int  photo_para_get_playindex2(void)
{	
	return  system_para.photo_para.play_hdmi_index;
}


int system_para_save(void)
{
	cJSON *r_para_t	= NULL;
	cJSON *v_para_t	= NULL;
	cJSON *a_para_t	= NULL;
	cJSON *p_para_t	= NULL;

	r_para_t = cJSON_CreateObject();

	cJSON_AddStringToObject(r_para_t, "id",   "rkplay"); 
	cJSON_AddStringToObject(r_para_t, "name", "rkplay"); 

	//**************************************************************
	v_para_t = cJSON_CreateObject();
	cJSON_AddItemToObject(r_para_t, "video_para", v_para_t); 

	
	cJSON_AddNumberToObject(v_para_t, "is_show",    system_para.video_para.is_show);	  
	cJSON_AddStringToObject(v_para_t, "play_path",  system_para.video_para.play_path);
	cJSON_AddNumberToObject(v_para_t, "path",       system_para.video_para.path);	  
	cJSON_AddNumberToObject(v_para_t, "play_mode",  system_para.video_para.play_mode);  
	cJSON_AddNumberToObject(v_para_t, "de_time",    system_para.video_para.de_time);
	cJSON_AddNumberToObject(v_para_t, "play_index", system_para.video_para.play_index);
	//****************************************************************************************
	p_para_t = cJSON_CreateObject();
	cJSON_AddItemToObject(r_para_t, "photo_para", p_para_t); 

	
	cJSON_AddStringToObject(p_para_t, "play_path",   system_para.photo_para.play_path);
	cJSON_AddNumberToObject(p_para_t, "path",        system_para.photo_para.path);	  
	cJSON_AddNumberToObject(p_para_t, "play_mode",   system_para.photo_para.play_mode);  
	cJSON_AddNumberToObject(p_para_t, "de_time",     system_para.photo_para.de_time);
	cJSON_AddNumberToObject(p_para_t, "edp_index",  system_para.photo_para.play_edp_index);
	cJSON_AddNumberToObject(p_para_t, "hdmi_index", system_para.photo_para.play_hdmi_index);

	//****************************************************************************************
	a_para_t = cJSON_CreateObject();
	cJSON_AddItemToObject(r_para_t, "audio_para", a_para_t); 


	cJSON_AddNumberToObject(a_para_t, "volume",  system_para.audio_para.volume);  
	//**************************************************************

	system_para_write_file(r_para_t,CONFIG_SAVE_PATH);
	//**************************************************************

	if(r_para_t != NULL){
	   cJSON_Delete(r_para_t);
	   r_para_t  = NULL;
	   v_para_t  = NULL;
	   a_para_t  = NULL;
	   p_para_t  = NULL;
	}
	//**************************************************************

	return 0;
}




int system_para_write_file(cJSON * root, const char* filename)
{
	int ret = -1;
	int fd  = -1;

	char *root_str = NULL;
	
	if(!root)
		return -1;

	if(!filename)
		return -1;

	root_str = cJSON_Print(root);
	if(!root_str)
		return -1;


	ret = remove(filename);

	fd = open(filename, O_CREAT|O_WRONLY);
	if(fd == (-1)){
		log_e("open file %s error\n",filename);
		return ;
	}

	write(fd,root_str,strlen(root_str));

	close(fd);
	fd = -1;

	return 0;
}


int system_para_get(void)
{
	FILE *fp = NULL;
	long long filelen = 0;
	char * root_str = NULL;
	
	cJSON *r_para_t	= NULL;

	cJSON *v_para_t	= NULL;
	cJSON *a_para_t	= NULL;
	cJSON *p_para_t = NULL;

	cJSON *c_is_show	= NULL;
	cJSON *c_play_path	= NULL;
	cJSON *c_path	    = NULL;
	cJSON *c_play_mode	= NULL;
	cJSON *c_de_time	= NULL;
	cJSON *c_play_index	= NULL;
	cJSON *c_play_index2= NULL;

	log_n("******************************************************\n");

	fp = fopen(CONFIG_SAVE_PATH,"rb");
	if(fp == NULL){
		
		log_e("no config file,%s\n",CONFIG_SAVE_PATH);
		system_para_default();
		system_para_save();
		return -1;
	}
	
	fseek(fp, 0, SEEK_END);   
	filelen = ftell(fp);	  
	fseek(fp,0,SEEK_SET);	

	log_n("filelen=%lld\n",filelen);
	//******************************************************
	if(filelen <= 0){
		fclose(fp);
		fp = NULL;

		system_para_default();
		system_para_save();
		return -1;
	}	

	root_str = malloc(filelen);
	if(root_str == NULL){
		fclose(fp);
		fp = NULL;

		system_para_default();
		system_para_save();
		return -1;
	}
	
	memset(root_str, '\0', filelen);
	fread(root_str,1,filelen,fp);
	fclose(fp);
	fp = NULL;

	log_n("root_str=%s\n",root_str);
	//******************************************************

	r_para_t = cJSON_Parse(root_str);
	free(root_str);
	root_str = NULL;

	if(!r_para_t){
		log_e("para error \n");
		return -1;
	}
	//******************************************************
	cJSON *c_id	= NULL;
	
	c_id = cJSON_GetObjectItem(r_para_t, "id");	
	if(!c_id){
		log_e("video para error\n");
		return -1;
	}
	
	log_n("c_id=%s\n",c_id->valuestring);
	//******************************************************

	v_para_t = cJSON_GetObjectItem(r_para_t, "video_para");	
	if(!v_para_t){
		log_e("video para error\n");
		return -1;
	}
	
	a_para_t = cJSON_GetObjectItem(r_para_t, "audio_para");	
	if(!a_para_t){
		log_e("audio para error\n");
		return -1;
	}	

	p_para_t = cJSON_GetObjectItem(r_para_t, "photo_para");	
	if(!p_para_t){
		log_e("photo para error\n");
		return -1;
	}
	//******************************************************
	//video para
	
	c_is_show     = cJSON_GetObjectItem(v_para_t, "is_show"); 
	c_play_path   = cJSON_GetObjectItem(v_para_t, "play_path"); 
	c_path        = cJSON_GetObjectItem(v_para_t, "path"); 
	c_play_mode   = cJSON_GetObjectItem(v_para_t, "play_mode"); 
	c_de_time     = cJSON_GetObjectItem(v_para_t, "de_time"); 
	c_play_index  = cJSON_GetObjectItem(v_para_t, "play_index"); 

	if(c_is_show){
		system_para.video_para.is_show = c_is_show->valueint;
	}

	if(c_play_path){
		strcpy(system_para.video_para.play_path,c_play_path->valuestring);
	}
	if(c_path){
		system_para.video_para.path = c_path->valueint;
	}
	if(c_play_mode){
		system_para.video_para.play_mode = c_play_mode->valueint;
	}
	if(c_de_time){
		system_para.video_para.de_time = c_de_time->valueint;
	}
	if(c_play_index){
		system_para.video_para.play_index = c_play_index->valueint;
	}
	
	log_n("is_show=%d\n",system_para.video_para.is_show);
	log_n("play_path=%s\n",system_para.video_para.play_path);
	log_n("path=%d\n",system_para.video_para.path);
	log_n("play_mode=%d\n",system_para.video_para.play_mode);
	log_n("de_time=%d\n",system_para.video_para.de_time);
	log_n("play_index=%d\n",system_para.video_para.play_index);
	//**********************************************************************************
	//photo para
	
	c_play_path  = cJSON_GetObjectItem(p_para_t, "play_path"); 
	c_path       = cJSON_GetObjectItem(p_para_t, "path"); 
	c_play_mode  = cJSON_GetObjectItem(p_para_t, "play_mode"); 
	c_de_time    = cJSON_GetObjectItem(p_para_t, "de_time"); 
	c_play_index = cJSON_GetObjectItem(p_para_t, "edp_index"); 
	c_play_index2 = cJSON_GetObjectItem(p_para_t, "hdmi_index"); 

	if(c_play_path){
		strcpy(system_para.photo_para.play_path,c_play_path->valuestring);
	}
	if(c_path){
		system_para.photo_para.path = c_path->valueint;
	}
	if(c_play_mode){
		system_para.photo_para.play_mode = c_play_mode->valueint;
	}
	if(c_de_time){
		system_para.photo_para.de_time = c_de_time->valueint;
	}
	if(c_play_index){
		system_para.photo_para.play_edp_index  = c_play_index->valueint;
	}
	if(c_play_index2){
		system_para.photo_para.play_hdmi_index = c_play_index2->valueint;
	}

	log_n("play_path=%s\n",system_para.photo_para.play_path);
	log_n("path=%d\n",system_para.photo_para.path);
	log_n("play_mode=%d\n",system_para.photo_para.play_mode);
	log_n("de_time=%d\n",system_para.photo_para.de_time);
	log_n("edp_index=%d\n",system_para.photo_para.play_edp_index);
	log_n("hdmi_index=%d\n",system_para.photo_para.play_hdmi_index);

	//**********************************************************************************
	cJSON *c_volume	= NULL;
	
	c_volume  = cJSON_GetObjectItem(a_para_t, "volume"); 

	if(c_volume){
		system_para.audio_para.volume = c_volume->valueint;
	}
	
	log_n("volume=%d\n",system_para.audio_para.volume);
	//**********************************************************************************
	log_n("******************************************************\n");

	return 0;
}


